package com.sxpi.zixun.controller.chapterlesson;

import com.sxpi.zixun.bo.ChapterLessonBo;
import com.sxpi.zixun.common.enums.ResultStatusEnum;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.controller.BaseController;
import com.sxpi.zixun.pojo.ChapterLesson;
import com.sxpi.zixun.service.chapterlesson.IChapterLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.pug.generator.anno.PugDoc;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 董伟豪
 * @Date: 2022/9/9 8:49
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@PugDoc(name = "章节管理", tabname = "kss_course_chapter_lesson")
public class ChapterLessonController extends BaseController {

    private final IChapterLessonService chapterlessonService;

    /**
     * 根据课程ID查询章信息
     * @param courseId
     * @return
     */
    @PostMapping("/chapterlesson/list/{courseId}")
    @PugDoc(name = "查询章节管理列表信息")
    public List<ChapterLessonBo> findChapterList(@PathVariable("courseId") Long courseId) {
        List<ChapterLessonBo> chapterList = chapterlessonService.findChapterList(courseId);
        if(!CollectionUtils.isEmpty(chapterList)){
            // 把下面的节也全部查出来
            chapterList = chapterList.stream().map(chapter->{
                List<ChapterLessonBo> lessonList = chapterlessonService.findLessonList(chapter.getId());
                chapter.setLessonList(lessonList);
                return chapter;
            }).collect(Collectors.toList());
        }
        return chapterList;
    }

    /**
     * 根据章节管理id查询明细信息
     * @param opid
     * @return
     */
    @PostMapping("/chapterlesson/get/{opid}")
    @PugDoc(name = "根据章节管理id查询明细信息")
    public ChapterLessonBo getChapterLessons(@PathVariable("opid") String opid) {
        Vsserts.isEmptyEx(opid, ResultStatusEnum.ID_NOT_EMPTY);
        // 本章信息
        ChapterLessonBo chapterLessonBo = chapterlessonService.getChapterLessonById(new Long(opid));
        if (Vsserts.isNotNull(chapterLessonBo)) {
            // 本章对应的节信息
            List<ChapterLessonBo> lessonList = chapterlessonService.findLessonList(chapterLessonBo.getId());
            chapterLessonBo.setLessonList(lessonList);
        }

        return chapterLessonBo;
    }

    /**
     * 拷贝章节信息
     * @param opid
     * @return
     */
    @PostMapping("/chapterlesson/copy/{opid}")
    @PugDoc(name = "保存和修改章节管理")
    public ChapterLessonBo copyChapterLesson(@PathVariable("opid") String opid) {
        ChapterLesson chapterLesson = chapterlessonService.getById(new Long(opid));
        if (chapterLesson != null) {
            chapterLesson.setId(null);
            chapterLesson.setStatus(0);
            chapterLesson.setIsdelete(0);
            return chapterlessonService.saveUpdateChapterLesson(chapterLesson);
        }
        return null;
    }

    /**
     * 保存和修改章节管理
     * @param chapterlesson
     * @return
     */
    @PostMapping("/chapterlesson/saveupdate")
    @PugDoc(name = "保存和修改章节管理")
    public ChapterLessonBo saveUpdateChapterLesson(@RequestBody ChapterLesson chapterlesson) {
        return chapterlessonService.saveUpdateChapterLesson(chapterlesson);
    }

    /**
     * 根据章节管理id删除章节管理
     * @param opid
     * @return
     */
    @PostMapping("/chapterlesson/delete/{opid}")
    @PugDoc(name = "根据章节管理id删除章节管理")
    public int deleteChapterLessonById(@PathVariable("opid") String opid) {
        Vsserts.isEmptyEx(opid,ResultStatusEnum.ID_NOT_EMPTY);
        return chapterlessonService.deleteChapterLessonById(new Long(opid));
    }

    /**
     * 物理删除章节信息
     * @param opid
     * @return
     */
    @PostMapping("/chapterlesson/remove/{opid}")
    @PugDoc(name = "物理删除章节信息")
    public int removeChapterLessonById(@PathVariable("opid") String opid) {
        Vsserts.isEmptyEx(opid,ResultStatusEnum.ID_NOT_EMPTY);
        return chapterlessonService.removeChapterLessonById(new Long(opid));
    }

    /**
     * 恢复删除
     * @param opid
     * @return
     */
    @PostMapping("/chapterlesson/reback/{opid}")
    @PugDoc(name = "恢复删除")
    public int rebackChapterLessonById(@PathVariable("opid") String opid) {
        Vsserts.isEmptyEx(opid,ResultStatusEnum.ID_NOT_EMPTY);
        return chapterlessonService.recoverChapterLessonById(opid);
    }
}
